﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;

namespace ConfigUpdateXml
{
    public partial class txtRemoteDir : Form
    {
        public txtRemoteDir()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtDateTime.Text = DateTime.Now.ToShortDateString();
        }
     
        

        private void btnDestFolder_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            txtDestFolder.Text = folderBrowserDialog1.SelectedPath;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        private void btnCreateXML_Click(object sender, EventArgs e)
        {
            //if (txtDestFolder.Text.Trim() == "")
            //{
            //    MessageBox.Show("请填写存放路径!");
            //    return;
            //}       
            StringBuilder str = new StringBuilder();
            str.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n");
            str.Append("<updater>\r\n");
            str.Append("<info>\r\n");
            str.Append("<appname>"+txtAppName.Text.Trim()+"</appname>\r\n");
            str.Append("<processname>"+txtProcessName.Text.Trim()+"</processname>\r\n");
            str.Append("<xmlurl>"+txtXmlUrl.Text.Trim()+"</xmlurl>\r\n");
            str.Append("<remotedir>"+txtRemotingDir.Text.Trim()+"</remotedir>");
            str.Append("<version>"+txtUpdateVersion.Text.Trim()+"</version>\r\n");
            str.Append("<datetime>"+txtDateTime.Text.Trim()+"</datetime>\r\n");
            str.Append("<isMustUpdate>"+(chkIsMustUpdate.Checked==true?"1":"0")+"</isMustUpdate>\r\n");
            str.Append("<desc><![CDATA["+txtUpdateInfo.Text.Trim()+"]]></desc>");
            str.Append("</info>\r\n");
            str.Append("<files>\r\n");
            DirectoryInfo dir = new DirectoryInfo(Application.StartupPath);
            foreach (FileInfo item in dir.GetFiles())
            {
                if (item.Extension != ".xml" && item.Name.ToLower().Contains("configupdatexml")==false && item.Name.ToLower().Contains("updater")==false)
                {
                    FileVersionInfo myFileVersion = FileVersionInfo.GetVersionInfo(item.FullName);
                    String strFileVersion = string.Format("{0}.{1}.{2}.{3}", myFileVersion.FileMajorPart, myFileVersion.FileMinorPart, myFileVersion.FileBuildPart, myFileVersion.FilePrivatePart);
                    //string strFileVersion = myFileVersion.FileVersion;
                    str.Append("<file>\r\n");
                    str.Append("<path>" + item.Name + "</path>\r\n");
                    str.Append("<version>" + strFileVersion + "</version>\r\n");
                    str.Append("</file>\r\n");
                }
            }
            str.Append("<file>\r\n");
            str.Append("<path>update.xml</path>\r\n");
            str.Append("<version>"+txtUpdateVersion.Text.Trim()+"</version>\r\n");
            str.Append("</file>\r\n");
            str.Append("</files>\r\n");
            str.Append("</updater>");
            Common.FileHelper.WriteFile(Application.StartupPath + "/update.xml", str.ToString(), "utf-8");
            MessageBox.Show("操作成功!");

        }

       
    }
}
